package com.qqls.youxiangousys.pj.sys.service.impl;

import com.qqls.youxiangousys.pj.common.entity.Pagination;
import com.qqls.youxiangousys.pj.common.util.Assert;
import com.qqls.youxiangousys.pj.sys.dao.SysOrderDao;
import com.qqls.youxiangousys.pj.sys.entity.SysOrder;
import com.qqls.youxiangousys.pj.sys.entity.SysUser;
import com.qqls.youxiangousys.pj.sys.service.SysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SysOrderServiceImpl implements SysOrderService {

    @Autowired
    private SysOrderDao orderDao;
    /**
     * 根据订单号查订单
     * @param orderNumber
     * @param curPage
     * @param pageSize
     * @param state
     * @return
     */
    public Pagination findOrder(String orderNumber, Integer curPage, Integer pageSize, Integer state,String startTime, String endTime) {
        //验证curPage和pageSize是否有值
        Assert.isEmpty(curPage == null || pageSize == null,"请选择当前页码和每页条数");
        //得到订单总条数
        int count = orderDao.countOrder(orderNumber,state);
        //创建分页对象，计算出所有属性
        Pagination pageObj = new Pagination(curPage,count,pageSize);
        curPage = pageObj.getCurPage();//得到当前页
        pageSize = pageObj.getPageSize();//得到每页条数
        int start =(curPage - 1) * pageSize;
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);*/
        List<SysUser> list =orderDao.findOrder(orderNumber,start,pageSize,state,startTime, endTime);//根据订单号找订单
        pageObj.setPageData(list);
        return pageObj;
    }

        public Integer updateState(Integer id, Integer state) {
            Assert.isEmpty(id == null || id == 0, "请选择要修改状态的商品！");
            Assert.isEmpty(state == null, "操作有误！");
            Integer n = orderDao.updateState(id, state);
            Assert.isEmpty(n == 0, "状态修改失败！");
            return n;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Integer deleteOrder(Integer id) {
        //1.验证参数
        Assert.isEmpty(id==null || id==0, "请选择");
        //2.调用三个dao的与角色相关的删除方法
        int n = orderDao.deleteOrder(id);
        Assert.isEmpty(n==0, "已删除!");
        //3.验证结果
        return n;
        }
}
